#include "audioout.h"

audioOut::audioOut(QObject *parent)
    :QObject (parent)
{
   output_stream = nullptr;
   output_Device = QAudioDeviceInfo::defaultOutputDevice();

   settings.setCodec("audio/pcm");
   settings.setSampleRate(44100);
   settings.setSampleType(QAudioFormat::SampleType(1));
   settings.setSampleSize(16);
   settings.setChannelCount(1);
   settings.setByteOrder(QAudioFormat::LittleEndian);

   if(!output_Device.isFormatSupported(settings))
       output_Device.nearestFormat(settings);

   audioOutput = new QAudioOutput(output_Device,settings,this);
   audioOutput->setBufferSize(4000);
   play_start();


}

audioOut::~audioOut()
{
    delete audioOutput;
    delete output_stream;
}

void audioOut::play_start()
{
    qDebug()<<"start playing...";

    output_stream = audioOutput->start();
}

void audioOut::play_stop()
{
    audioOutput->stop();
    output_stream = nullptr;
}

void audioOut::play_data(const QByteArray &data)
{
    if(output_stream==nullptr){return;}
    output_stream->write(data);
}

